package jetbrains.buildServer.cuta.logic.operators;

import jetbrains.buildServer.cuta.logic.ColumnDef;
import jetbrains.buildServer.cuta.logic.sheet.Cell;
import org.jdom.Element;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;



/**
 * @author Leonid Bushuev from JetBrains
 */
public class SequenceOperator extends AbstractOperator
{

    private final List<AbstractOperator> operators =
            new ArrayList<AbstractOperator>();


    @Override
    public void init(Element element, ColumnDef column, ExternalContext extContext)
    {
        List children = element.getChildren();
        for (Object child: children)
        {
            Element inner = (Element) child;
            AbstractOperator operator =
                    AllOperators.makeOperator(inner, column, extContext);
            operators.add(operator);
        }
    }


    @Override
    public void run(Object object, Cell cell)
    {
        for (AbstractOperator operator: operators)
            operator.run(object, cell);
    }


    @Override
    @NotNull
    public String name()
    {
        return "sequence";
    }


    @Override
    public String toString()
    {
        StringBuilder buf = new StringBuilder();
        buf.append("sequence of");
        for (AbstractOperator operator: operators)
            buf.append(' ').append(operator.name());
        return buf.toString();
    }
}
